<?php
class LogType{
	const Error = 0;
	const Info = 1;
	const Debug = 2;
}

class Logger{
	
	public $Email;
	public $FileName;
	public $logger;
	public $LogType;
	
	function __construct($FileName = "",$Email ="") {
		$this->FileName 	= 	$FileName;
		$this->Email 		= 	$Email;
	}
	
	public function Log($LogType = LogType::Error,$ex){
	
		$this->LogType = $LogType;
		$this->CreateLogDir();
		switch($this->LogType){
			case LogType::Info:
				$data = "Server:" .$_SERVER['SERVER_NAME'] ."\r\nInfo:".$ex." \t".date('Y-d-H-i')."\r\n";
				break;
			case LogType::Debug:
				$data = $_SERVER['SERVER_NAME'] ."\r\nDebug:".$ex." \t".date('Y-d-H-i')."\r\n";
			default:
				$data = $_SERVER['SERVER_NAME'] ."\r\nError:".$ex." \t".date('Y-d-H-i')."\r\n";
				break;
		}
		if ($this->FileName != ''){
			$logFileName = $this->FileName."-".date('Y-m-d-H-i').".txt";
			if (file_exists('logFiles/'.$logFileName)) {
				$logFile = fopen('logFiles/'.$logFileName,'a');
			} else {
				$logFile = fopen('logFiles/'.$logFileName,'w');
			}
			fwrite($logFile,$data);
			fclose($logFile);
		}
		if ($this->Email !=''){
			$to = $this->Email;
 			$subject = "Error In Application";
			$body = $data;
 			mail($to, $subject, $body);
		}
	}
	public function CreateLogDir(){
		if (is_Dir('LogFiles') != true){
			mkdir('LogFiles');
		}
	}
	
}
